package com.typing.alarm.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import com.typing.alarm.database.DBManager;
import com.typing.alarm.schedule.AlarmClock;
import com.typing.alarm.value.TypingAlarmVO;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class TypingAlarmService extends Service{
	
	private DBManager dbMgr;
	private Context mContext;
	private ArrayList<TypingAlarmVO> list;
	private HashMap<String,AlarmClock> alarmMap;
	
	private ITypingAlarmService.Stub serviceStub = new ITypingAlarmService.Stub() {    	
		@Override
		public void stopRepeat(String alarmId) throws RemoteException {
			// TODO Auto-generated method stub
			AlarmClock clock = alarmMap.get(alarmId);
			clock.stopRepeat();
		}
		@Override
		public void refleshAlarm() throws RemoteException {
			// TODO Auto-generated method stub
			
			bindData();			
			setAlarm();			
		}
    };    
    
	private void bindData(){		
		dbMgr.open();
    	// rawQuery 함수를 이용한 데이터 질의
		list = dbMgr.getAppInfo("SELECT * FROM "+DBManager.DATABASE_TABLE + " ORDER BY "+DBManager.KEY_ID+" ASC", null);		
	}
	private void setAlarm(){		
		if(list == null || list.size() == 0) return;		
		clearAlarm();
		for(int i=0 ; i < list.size(); i++){
			TypingAlarmVO data = list.get(i);
			if("Y".equals(data.getUseYn())){
				AlarmClock clock = new AlarmClock( mContext, data, 1*60*1000);				
				clock.start();
				alarmMap.put(data.getAlarmId(), clock);
				Log.d("setAlarm - set",data.getAlarmId());
			}
		}		
	}
	
	private void clearAlarm(){		
		if(alarmMap.size() == 0) return;
		Set<String> set = alarmMap.keySet();		
		Iterator<String> itor = set.iterator();
		while(itor.hasNext()){
			String key = itor.next();
			AlarmClock clock = alarmMap.get(key);
			clock.cancel();
			Log.d("clearAlarm - remove",key);			
		}	
		alarmMap.clear();		
	}
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		Log.d("AlarmService - onCreate","1");
		mContext = this;
		dbMgr = new DBManager(this);		
		alarmMap = new HashMap<String,AlarmClock>();
		Log.d("AlarmService - onCreate","2");
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		Log.d("AlarmService - onBind","1");
		return serviceStub;
	}
	@Override
	public void onRebind(Intent intent) {
		// TODO Auto-generated method stub
		Log.d("AlarmService - onRebind","1");
		super.onRebind(intent);
	}	
	@Override
	public boolean onUnbind(Intent intent) {
		// TODO Auto-generated method stub
		Log.d("AlarmService - onUnbind","1");
		return super.onUnbind(intent);
	}	
	@Override    
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);     
		Log.d("AlarmService - onStart","1");
		bindData(); 
		setAlarm();
		Log.d("AlarmService - onStart","2");
	}       
	@Override    
	public void onDestroy() {         
		super.onDestroy();   
		Log.d("AlarmService - onDestroy","1");			
		list = null;
		clearAlarm();
		alarmMap = null;
		Log.d("AlarmService - onDestroy","2");			
	}	
}
